<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 * 
 */
 
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/team.class.php");
require_once(ROOT_PATH."class/temporal.class.php");

$team = new team;
$team->status=STATUS_ACTIVE;

if (isset($CURRENT_USER) && $CURRENT_USER->groupid == GROUP_COORD) {
	$team->id=$CURRENT_USER->teamid;
	$loader->get($team);
	$list = array($team);	
} else {
	$tm->assign("hidemenu","true");
	$list = $loader->loadListBy($team,$team->listByStatus());	
}

if ($param['auth']!="") {
	$temporal = new temporal;
	$temporal->id=$param['auth'];
	$ret = $loader->get($temporal);
	if ($ret===false) {
		$error=$LANG['codeexpire'];
	} else {
		$tm->assign("AUTH_CODE",$temporal->id);
		$tm->assign("AUTH_MAIL",$temporal->extra);
		$tm->assign("AUTH_TEAM",$temporal->teamid);
	}
} else {
	$ret = true;
}

if ($ret!==false) {

	$tm->assign('timezonelist',$TIMEZONE_LIST);
	$tm->assign('langlist',$LANGUAGE_LIST);
	$tm->assign('teamlist',$list);
	$tm->assign('param',$param);
	$tm->assign('DISABLE_REG',DISABLE_REG);
	
	if ($param['op']=='add' and $flow_ok) {
		if (isEmpty($param['userid'])
			or
			isEmpty($param['mail'])
			or
			isEmpty($param['remail'])
			or
			/*isEmpty($param['password'])
			or
			isEmpty($param['repassword'])
			or*/
			isEmpty($param['lang'])
			or
			(isEmpty($param['team']) && $CURRENT_USER->groupid != GROUP_ADMIN)
			or
			isEmpty($param['timezone'])
			) {
			$error = $LANG['regmissing'];
			$tm->assign('error',$error);		
			$content = $tm->fetch('reguser.tpl');
			/*
		} elseif ($param['password']!=$param['repassword']) {
			$error = $LANG['passnotmatch'];
			$content = $tm->fetch('reguser.tpl');
			*/
		} elseif ($param['mail']!=$param['remail']) {
			$error = $LANG['mailnotmatch'];
			$content = $tm->fetch('reguser.tpl');
		} elseif (!isValidEmail($param['mail'])) {
			$error = $LANG['invalidmail'];
			$content = $tm->fetch('reguser.tpl');
		} elseif (strlen($param['userid'])<MIN_USER_LEN) {
			$error = $LANG['useridtooshort'];
			$content = $tm->fetch('reguser.tpl');
		} elseif (strlen($param['userid'])>MAX_USER_LEN) {
			$error = $LANG['useridtoolong'];
			$content = $tm->fetch('reguser.tpl');
	/*
		} elseif (strlen($param['password'])<MIN_PASSWORD_LEN) {
			$error = $LANG['passtooshort'];
			$content = $tm->fetch('reguser.tpl');*/
		} else {
			$us = new user;
			$us->userid=$param['userid'];
			$ret = $loader->get($us);
			if ($ret) {
				$error = $LANG['userexists'];
				$content = $tm->fetch('reguser.tpl');
			} else {
				$pwd = util::hashcode(util::createPassword(10));
				
				$us->mail=$param['mail'];
				//$us->password=$param['password'];
				$us->password=$pwd;
				$us->lang=$param['lang'];
				$us->teamid=$param['team'];
				$us->groupid=GROUP_USER;		
				$us->timezone=$param['timezone'];
				$us->status=INITIAL_USER_STATE;
				
				$us->passcode = $pwd;
				$us->passexpire = util::now();			
	
				$ret = $loader->save($us);
	
				if ($temporal!=null) $loader->delete($temporal);
				
				updateLang($us->lang);
	
				sendTeamMail($us);
				sendConfirmMail($us,$pwd);
			
				if (INITIAL_USER_STATE == STATUS_ACTIVE) {
					regReceived($us,'regreceived');
					$message = $LANG['regcomplete'];
				} else {
					regReceived($us,'regreceivedinactive');
					$message = $LANG['regcompleteinactive'];
				}
				/*$content = createMessage($homelink,array($LANG['home']));*/
			}
		}
	} else {
		$content = $tm->fetch('reguser.tpl');
	}
	
}


?>
